<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Memberorder extends Model
{
    protected $table = 'member_order';
    protected $primaryKey = 'id';

    public static function userMemberOrder($openid)
    {
        return self::select("month","title","price","end_time","pay_status","status","created_at")->where("openid",$openid)->where("pay_status",2)->paginate(10);
    }
    
    public static function userLastMember($openid)
    {
        return self::where("openid",$openid)->where("pay_status",2)->first();
    }

    public static function userMenberLists($openids)
    {
        return self::whereIn("openid",$openids)->where("pay_status",2)->orderBy("id","DESC")->groupBy("openid")->get();
    }

    public static function adminMemberLists($pay_status)
    {
        $orders = self::select('member_order.*','users.code','users.nike')
            ->leftjoin('users', 'users.openid', '=', 'member_order.openid')
            ->where("member_order.pay_status", $pay_status);
        return $orders->orderBy('member_order.id', 'desc')->paginate(25);
    }

    public static function getByOrderNo($out_trade_no)
    {
        return self::where("out_trade_no",$out_trade_no)->where("pay_status",1)->first();
    }

    public static function addOrder($openid,$out_trade_no,$title,$end_time,$month,$price,$status,$appid)
    {
        return self::insert(["openid"=>$openid,"out_trade_no"=>$out_trade_no,"title"=>$title,"end_time"=>$end_time,"month"=>$month,"price"=>$price,"status"=>$status,"appid"=>$appid,"created_at"=>time()]);
    }

    public static function updatePayStatus($out_trade_no,$data)
    {
        return self::where("out_trade_no",$out_trade_no)->update($data);
    }
}